using System;
using TMPro;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

namespace GameCore.UI
{
    public class AdventureUI : MonoBehaviour
    {
        public SimpleAdventureRegion region;
        public AreaTagUI areaTagPrefab;
        public AreaTagUI selected;
        
        #region Adventure Progress
        [Header("Adventure Progress")]
        public ProgressBar progressBar;
        public Sprite playerIconSprite;
        public Sprite signSprite;
        public ProgressBarIcon playerIcon;

        public void ResetProgress()
        {
            progressBar.ClearIcons();
            progressBar.SetCurrentProgress(0);
            playerIcon = progressBar.AddIcon(playerIconSprite, 0);
        }

        public void SetTotalProgress(int progress)
        {
            progressBar.SetTotalProgress(progress);
        }

        public void SetProgress(int progress, float time)
        {
            progressBar.SetCurrentProgress(progress, time);
            playerIcon.SetProgress(progress, time);
        }

        public void AddSign(int position)
        {
            progressBar.AddIcon(signSprite, position);
        }
        #endregion
        
        #region Area Selection
        [Header("Area Selection")]
        public GameObject panel_AreaSelection;
        public List<AreaTagUI> areaTags;
        public TMP_Text txt_RegionTitle;
        public TMP_Text txt_AreaTitle;
        public TMP_Text txt_AreaDescription;
        public Button btn_GoToArea;
        
        public void ShowAreaSelectionPanel(bool flag)
        {
            panel_AreaSelection.SetActive(flag);
            if (flag) PlayerInputHandler.Block(panel_AreaSelection);
            else PlayerInputHandler.Unblock(panel_AreaSelection);
            if (!flag) return;
            foreach (var areaTag in areaTags)
            {
                if (region.AllowTransition(areaTag.areaConfig.areaID)) areaTag.SetTagActive();
                else areaTag.SetTagInactive();
            }
            SetSelectedArea(currSelectedTag);
        }

        public void CreateAreaTag(AreaConfig areaConfig)
        {
            var tag = Instantiate(areaTagPrefab, panel_AreaSelection.transform);
            tag.areaConfig = areaConfig;
        }

        private AreaTagUI currSelectedTag = null;
        public void SetSelectedArea(AreaTagUI tag)
        {
            currSelectedTag?.Deselect();
            if (tag is null) return;
            txt_AreaTitle.text = tag.areaConfig.areaID.id;
            currSelectedTag = tag;
            btn_GoToArea.interactable = region.AllowTransition(tag.areaConfig.areaID);
            tag.Select();
        }

        private void GoToNewArea()
        {
            if (currSelectedTag == null) return;
            region.SetCurrentArea(currSelectedTag.areaConfig.areaID);
            ShowAreaSelectionPanel(false);
        }
        
        #endregion

        #region Stamina
        
        public TMP_Text txt_Stamina;
        public ProgressBar bar_Stamina;
        public Button btn_MoveForward;
        public Button btn_Evacuate;

        public void SetStamina(int stamina)
        {
            bar_Stamina.SetCurrentProgress(stamina);
            bar_Stamina.SetTotalProgress(stamina);
            txt_Stamina.text = $"<size=32>{stamina}";
        }

        public void SetStamina(int stamina, int max)
        {
            bar_Stamina.SetCurrentProgress(stamina);
            bar_Stamina.SetTotalProgress(max);
            txt_Stamina.text = $"<size=32>{stamina}<size=24>/{max}";
        }

        private void MoveForward()
        {
            region.MoveForward();
        }

        public void ShowEvacuateButton(bool flag)
        {
            btn_Evacuate.gameObject.SetActive(flag);
        }

        private void Evacuate()
        {
            region.Evacuate();
        }
        
        #endregion

        private void Start()
        {
            btn_GoToArea.onClick.AddListener(GoToNewArea);
            btn_MoveForward.onClick.AddListener(MoveForward);
            btn_Evacuate.onClick.AddListener(Evacuate);
        }

        public PanelAdventureMenu menu;

        private void OnEnable()
        {
            PlayerInputHandler.AddKeyDownListener(KeyCode.Escape, ToggleMenu);
        }

        private void OnDisable()
        {
            PlayerInputHandler.RemoveKeyDownListener(KeyCode.Escape, ToggleMenu);
        }
        
        private void ToggleMenu() => menu.gameObject.SetActive(!menu.gameObject.activeSelf);
    }
}